﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Spring.Context;
using Spring.Context.Support;
using XiDainChangAn.OA.IBLL;

namespace XiDainChangAn.OA.WF
{

    public sealed class SetStepActivity : CodeActivity
    {
        // 定义一个字符串类型的活动输入参数
        public InArgument<string> StepName { get; set; }
        public InArgument<bool> IsEnd { get; set; }


        // 如果活动返回值，则从 CodeActivity<TResult>
        // 并从 Execute 方法返回该值。
        protected override void Execute(CodeActivityContext context)
        {
            // 获取 Text 输入参数的运行时值
            string text = context.GetValue(this.StepName);
            bool end = context.GetValue(this.IsEnd);


            Guid insId = context.WorkflowInstanceId;
            //更新步骤名
            //根据流程id取得流程实例
            IApplicationContext ctx = ContextRegistry.GetContext();
            var WF_InstanceService = ctx.GetObject("WF_InstanceService") as IWF_InstanceService;
            var WF_StepService = ctx.GetObject("WF_StepService") as IWF_StepService;
            //根据流程实例id 和当前状态读取当前步骤
            var instance = WF_InstanceService.GetEntitys(u => u.WFInstanceId == insId).FirstOrDefault();


            //报错的地方  
            if (instance != null)
            {
                var stepStatus = (short)XiDianChangAn.OA.Model.Enum.WFStemEnum.NoProcessed;
                var step = instance.WF_Step.Where(s => s.StepStatus == stepStatus).FirstOrDefault();

                step.StepName = text;
                step.IsEndStep = end == false ? (short)0 : (short)1;

                //是不是结束步骤更新一下
                if (end)
                {
                    //步骤的结果 Result=“审核结果”
                    step.ProcessResult = "审批结束";
                    instance.Status = (short)XiDianChangAn.OA.Model.Enum.WFStemEnum.Processed;
                    //更新实例表中的状态
                    WF_InstanceService.UpDate(instance);
                }
                // 更新步骤信息
                WF_StepService.UpDate(step);
            }
        }
    }
}
